package com.shq.ex1.bus.activity;

import java.util.ArrayList;
import java.util.List;

import com.shq.ex1.bus.util.UtilMethod;
import com.shq.ex1.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ZZhanResultActivity extends Activity {

	private ListView zzhanResult = null;
	private ArrayAdapter<String> adapter = null;
	private StringBuffer ChangeInfo = null;
	private StringBuffer ChangeDInfo = null;
	private List<String> changeList = null;
	private TextView zhandianText01 = null;
	private TextView zhandianText02 = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zzhan_result);
		zzhanResult = (ListView) findViewById(R.id.zzhanResult);
		zhandianText01 = (TextView) findViewById(R.id.zhandian01);
		zhandianText02 = (TextView) findViewById(R.id.zhandian02);
		Intent intent = getIntent();
		final String databasePath = intent.getStringExtra("databasePath");
		String zhandian01 = intent.getStringExtra("zhandian01");
		String zhandian02 = intent.getStringExtra("zhandian02");
		zhandianText01.setText(zhandian01);
		zhandianText02.setText(zhandian02);
		List<String> list = UtilMethod.getDChanged(databasePath, zhandian01, zhandian02);
		List<String> list01 = new ArrayList<String>();
		List<String> list02 = new ArrayList<String>();

		if (list != null && !list.isEmpty()) {
			System.out.println("直达");
			List<String> listInfo = new ArrayList<String>();
			for (String string : list) {
				ChangeDInfo = new StringBuffer();
				ChangeDInfo.append(string + "\n");
				ChangeDInfo.append("直达");
				int count = UtilMethod.getZhanCount(databasePath, zhandian01, zhandian02, string);
				ChangeDInfo.append("(共"+count+"站)");
				listInfo.add(ChangeDInfo.toString());
			}
			adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listInfo);
			zzhanResult.setAdapter(adapter);
		}else{
			changeList = new ArrayList<String>();
			List<String> zhans = UtilMethod.getChangedZhan(databasePath, zhandian01, zhandian02);
			for (String string : zhans) {
				list01 = UtilMethod.getDChanged(databasePath, zhandian01, string);
				list02 = UtilMethod.getDChanged(databasePath, string, zhandian02);
				for (String string01 : list01) {
					ChangeInfo = new StringBuffer();
					int count01 = UtilMethod.getZhanCount(databasePath, zhandian01, string, string01);
					System.out.println(count01 + "----------------");
					//System.out.println("从"+zhandian01+"出发，乘坐" + string01 +"到"+string +"下车" );
					//ChangeInfo.append("从"+zhandian01+"出发，乘坐" + string01 +"到"+string +"下车");
					ChangeInfo.append(string01 + "换乘");
					for (String string2 : list02) {
						//System.out.println("在" +string+"乘坐"+string2+"到达目的地" +zhandian02 );
						//ChangeInfo.append("在" +string+"乘坐"+string2+"到达目的地" +zhandian02 );
						int count02 = UtilMethod.getZhanCount(databasePath, string, zhandian02, string2);
						System.out.println(count02 + "==============");
						ChangeInfo.append(string2+"\n" );
						ChangeInfo.append("换乘一次(" +string+"  换乘"+")\n共" + (count01 + count02) +"站");

					}
					String ChangeInfoStr = ChangeInfo.toString();
					changeList.add(ChangeInfoStr);
				}
			}
			adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,changeList);
			zzhanResult.setAdapter(adapter);
		}
	}

}
